package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.zxing.WriterException;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.store.adapter.OneLotteryParticipateAdapter;
import com.meitianhui.convenitenbuy.module.store.contract.OneLotteryParticipateContract;
import com.meitianhui.convenitenbuy.module.store.di.DaggerStoreComponent;
import com.meitianhui.convenitenbuy.module.store.di.StoreModule;
import com.meitianhui.convenitenbuy.module.store.model.OneLotteryParticipateBean;
import com.meitianhui.convenitenbuy.module.store.model.StoreOneLotteryBean;
import com.meitianhui.convenitenbuy.module.store.presenter.OneLotteryParticipatePresenter;
import com.meitianhui.convenitenbuy.push.model.OneLotteryPushBean;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.zxing.encoding.EncodingHandler;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/11/24.
 * 一元抽奖参与
 */

public class OneLotteryParticipateActivity extends BaseToolbarActivity implements OneLotteryParticipateContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    private RecyclerView mRv;
    private RelativeLayout mRlNone;
    @Inject
    OneLotteryParticipatePresenter presenter;
    private OneLotteryParticipateAdapter mAdapter;
    private int pageNum = 1;
    private StoreOneLotteryBean.ActivityListEntity entity;
    private OneLotteryPushBean mBean;
    private Dialog dialog;


    @Override
    protected int setView() {
        return R.layout.activity_one_lottery_participate;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerStoreComponent
                .builder()
                .appComponent(App.getInstance().getAppComponent())
                .storeModule(new StoreModule(this))
                .build()
                .inject(this);
        mRv = getViewById(R.id.rv_one_lottery_participate);
        mRlNone = getViewById(R.id.rl_one_lottery_participate_none);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(layoutManager);

        initData();
        initInfo();
        registerPush();
    }

    private void registerPush() {
        Disposable disposable = RxBus.get().toFlowable(OneLotteryPushBean.class).subscribe(new Consumer<OneLotteryPushBean>() {
            @Override
            public void accept(OneLotteryPushBean bean) throws Exception {
                if (bean != null) {
                    if (bean.getType() == 0) {
                        mBean = bean;
                        showDialog(bean.getUrl());
                    } else if (bean.getType() == 1) {
                        String pi = bean.getProcess_id();
                        if (!TextUtils.isEmpty(pi)) {
                            if (TextUtils.equals(pi, mBean.getProcess_id()) && dialog != null) {
                                dialog.dismiss();
                                if (!TextUtils.isEmpty(bean.getUrl())) {
                                    TipUtils.showToast(OneLotteryParticipateActivity.this, bean.getUrl(), 3);
                                }
                                int position = mBean.getPosition();
                                OneLotteryParticipateBean.ListEntity item = mAdapter.getItem(position);
                                if(item!=null){
                                    item.setStatus("exchanged");
                                    mAdapter.setData(position, item);
                                }
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }

    private void showDialog(String url) {
        dialog = new Dialog(this, R.style.dialog_custom);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(R.layout.dialog_one_lottery_participate);
        dialog.show();
        ImageView img = (ImageView) dialog.findViewById(R.id.dig_ewm);
        try {
            Bitmap qrCodeBitmap = EncodingHandler.createQRCode(url, 400);
            img.setImageBitmap(qrCodeBitmap);
        } catch (WriterException e) {
            e.printStackTrace();

        }
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }

    private void initInfo() {
        presenter.getParticipate(pageNum);
    }


    private void initData() {
        setTbTitle(getString(R.string.home_one_lottery_participate_mine));
    }

    @Override
    protected void onRefreshData() {
        if (mAdapter != null && mAdapter.isLoading())
            mAdapter.loadMoreEnd();
        pageNum = 1;
        initInfo();
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getParticipateBack(boolean result, String msg, OneLotteryParticipateBean data) {
        if (!result) {
            TipUtils.showToast(App.getInstance(), msg);
        } else {
            if (mAdapter == null) {
                mAdapter = new OneLotteryParticipateAdapter(this, data.getList(), data.getDoc_url());
                mRv.setAdapter(mAdapter);
                mAdapter.setOnLoadMoreListener(this, mRv);
            } else {
                if (mAdapter.isLoading())
                    mAdapter.loadMoreComplete();
                if (pageNum == 1)
                    mAdapter.setNewData(data.getList(), data.getDoc_url());
                else mAdapter.addNewData(data.getList(), data.getDoc_url());
            }
            mAdapter.setEnableLoadMore(data.getList().size() < 20 ? false : true);
            mRlNone.setVisibility(data.getList().isEmpty() && pageNum == 1 ? View.VISIBLE : View.GONE);
            refreshFinish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter != null && mAdapter.isLoading())
            mAdapter.loadMoreEnd();
        refreshFinish();
    }

    @Override
    public void onLoadMoreRequested() {
        pageNum++;
        initInfo();
    }
}
